cmake_minimum_required(VERSION 3.14)
project(CompleteBinaryTree VERSION 1.0.0 LANGUAGES CXX)

# Project options
option(BUILD_EXAMPLES "Build example programs" ON)
option(BUILD_TESTS "Build unit tests" OFF)

# C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Interface library (header-only)
add_library(CompleteBinaryTree INTERFACE)
add_library(CompleteBinaryTree::CompleteBinaryTree ALIAS CompleteBinaryTree)

target_include_directories(CompleteBinaryTree
    INTERFACE
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
        $<INSTALL_INTERFACE:include>
)

target_compile_features(CompleteBinaryTree INTERFACE cxx_std_17)

# Compiler warnings
if(MSVC)
    target_compile_options(CompleteBinaryTree INTERFACE /W4 /WX)
else()
    target_compile_options(CompleteBinaryTree INTERFACE 
        -Wall -Wextra -Wpedantic -Werror
    )
endif()

# Examples
if(BUILD_EXAMPLES)
    add_subdirectory(examples)
endif()

# Tests
if(BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()

# Installation
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

install(FILES complete_binary_tree.hpp
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(TARGETS CompleteBinaryTree
    EXPORT CompleteBinaryTreeTargets
)

install(EXPORT CompleteBinaryTreeTargets
    FILE CompleteBinaryTreeTargets.cmake
    NAMESPACE CompleteBinaryTree::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CompleteBinaryTree
)

configure_package_config_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CompleteBinaryTreeConfig.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/CompleteBinaryTreeConfig.cmake
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CompleteBinaryTree
)

write_basic_package_version_file(
    ${CMAKE_CURRENT_BINARY_DIR}/CompleteBinaryTreeConfigVersion.cmake
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY SameMajorVersion
)

install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/CompleteBinaryTreeConfig.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/CompleteBinaryTreeConfigVersion.cmake
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CompleteBinaryTree
)

# Export for use from the build tree
export(EXPORT CompleteBinaryTreeTargets
    FILE ${CMAKE_CURRENT_BINARY_DIR}/CompleteBinaryTreeTargets.cmake
    NAMESPACE CompleteBinaryTree::
)
